<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\DB;



class ShopController extends Controller
{
    public function index($id) {
    	// dd($id);
    	//根据商家id查询对应商家信息和商品
    	$businesses = DB::table('businesses')
    		-> where('id',$id)
    		-> get();

//    		 dd($businesses[0] -> cateid);
    	$cates = DB::table('cates')
    		-> where('id',$businesses[0] -> cateid)
//    		-> where('name',$businesses[0] -> username)
    		-> get();
//    	 dd($cates[0] -> id);
    	$goods = DB::table('goods')
    		-> where('cateid',$cates[0] -> id)
    		-> get();
//    		 dd($goods);

        return view('home.shop.index',['businesses' => $businesses,'cates' => $cates,'goods' => $goods]);
    }

    public function shopajax(Request $request) {

        $data = $request->all();


        $ins_id = DB::table('details')->insertGetID(
            ['goods_id' => $request->all()['good_id'], 'num' => 1]
        );

        $data = array(
            'good_id' =>  $request->all()['good_id'],
            'num' =>  $request->all()['num'],
            'details_id' =>  $ins_id
        );

        $detail = array();
        array_push($detail, $data);


        $request->session()->put('details', $detail);
//        dd($request->session()->get('details')[0]['num']);
        // 得到商品信息
//        $data = DB::table('goods')->where('id', $request->session()->get('good_id'));
        $res = DB::table('goods')->where('id', $request->good_id)->first();

//        dd($res);
        return response()->json($res);
//        dd($request->session()->all());
//        return response()->json($request->session()->get('details'));
    }
}
